package edu.hcmus.sow.domain;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.codehaus.jackson.annotate.JsonIgnore;

/**
 */

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllTopics", query = "select myTopic from Topic myTopic"),
		@NamedQuery(name = "findTopicByContent", query = "select myTopic from Topic myTopic where myTopic.content = ?1"),
		@NamedQuery(name = "findTopicByCreatedDate", query = "select myTopic from Topic myTopic where myTopic.createdDate = ?1"),
		@NamedQuery(name = "findTopicById", query = "select myTopic from Topic myTopic where myTopic.id = ?1"),
		@NamedQuery(name = "findTopicByPrimaryKey", query = "select myTopic from Topic myTopic where myTopic.id = ?1"),
		@NamedQuery(name = "findTopicByTitle", query = "select myTopic from Topic myTopic where myTopic.title = ?1"),
		@NamedQuery(name = "findTopicByTitleContaining", query = "select myTopic from Topic myTopic where myTopic.title like ?1"),
		@NamedQuery(name = "findTopicByUpdatedDate", query = "select myTopic from Topic myTopic where myTopic.updatedDate = ?1") })
@Table(catalog = "sow", name = "topic")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "SharedOnlineWorkspace/edu/hcmus/sow/domain", name = "Topic")
@XmlRootElement(namespace = "SharedOnlineWorkspace/edu/hcmus/sow/domain")
public class Topic implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 */

	@Column(name = "ID", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@XmlElement
	Integer id;
	/**
	 */

	@Column(name = "TITLE", length = 200, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String title;
	/**
	 */

	@Column(name = "CONTENT", nullable = false, columnDefinition = "TEXT")
	@Basic(fetch = FetchType.EAGER)
	@Lob
	@XmlElement
	String content;
	/**
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATED_DATE")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar createdDate;
	/**
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "UPDATED_DATE")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar updatedDate;

	/**
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "CREATED_BY", referencedColumnName = "ID") })
	@XmlTransient
	User user;
	/**
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "FORUM_ID", referencedColumnName = "ID", nullable = false) })
	@XmlTransient
	Forum forum;
	/**
	 */
	@OneToMany(mappedBy = "topic", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	java.util.Set<edu.hcmus.sow.domain.TopicPoll> topicPolls;
	/**
	 */
	@OneToMany(mappedBy = "topic", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	java.util.Set<edu.hcmus.sow.domain.TopicComment> topicComments;

	/**
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 */
	public Integer getId() {
		return this.id;
	}

	/**
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 */
	public String getTitle() {
		return this.title;
	}

	/**
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 */
	public String getContent() {
		return this.content;
	}

	/**
	 */
	public void setCreatedDate(Calendar createdDate) {
		this.createdDate = createdDate;
	}

	/**
	 */
	public Calendar getCreatedDate() {
		return this.createdDate;
	}

	/**
	 */
	public void setUpdatedDate(Calendar updatedDate) {
		this.updatedDate = updatedDate;
	}

	/**
	 */
	public Calendar getUpdatedDate() {
		return this.updatedDate;
	}

	/**
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 */
	@JsonIgnore
	public User getUser() {
		return user;
	}

	/**
	 */
	public void setForum(Forum forum) {
		this.forum = forum;
	}

	/**
	 */
	@JsonIgnore
	public Forum getForum() {
		return forum;
	}

	/**
	 */
	public void setTopicPolls(Set<TopicPoll> topicPolls) {
		this.topicPolls = topicPolls;
	}

	/**
	 */
	@JsonIgnore
	public Set<TopicPoll> getTopicPolls() {
		if (topicPolls == null) {
			topicPolls = new java.util.LinkedHashSet<edu.hcmus.sow.domain.TopicPoll>();
		}
		return topicPolls;
	}

	/**
	 */
	public void setTopicComments(Set<TopicComment> topicComments) {
		this.topicComments = topicComments;
	}

	/**
	 */
	@JsonIgnore
	public Set<TopicComment> getTopicComments() {
		if (topicComments == null) {
			topicComments = new java.util.LinkedHashSet<edu.hcmus.sow.domain.TopicComment>();
		}
		return topicComments;
	}

	/**
	 */
	public Topic() {
	}

	/**
	 * Copies the contents of the specified bean into this bean.
	 *
	 */
	public void copy(Topic that) {
		setId(that.getId());
		setTitle(that.getTitle());
		setContent(that.getContent());
		setCreatedDate(that.getCreatedDate());
		setUpdatedDate(that.getUpdatedDate());
		setUser(that.getUser());
		setForum(that.getForum());
		setTopicPolls(new java.util.LinkedHashSet<edu.hcmus.sow.domain.TopicPoll>(that.getTopicPolls()));
		setTopicComments(new java.util.LinkedHashSet<edu.hcmus.sow.domain.TopicComment>(that.getTopicComments()));
	}

	/**
	 * Returns a textual representation of a bean.
	 *
	 */
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");
		buffer.append("title=[").append(title).append("] ");
		buffer.append("content=[").append(content).append("] ");
		buffer.append("createdDate=[").append(createdDate).append("] ");
		buffer.append("updatedDate=[").append(updatedDate).append("] ");

		return buffer.toString();
	}

	/**
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	/**
	 */
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Topic))
			return false;
		Topic equalCheck = (Topic) obj;
		if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
			return false;
		if (id != null && !id.equals(equalCheck.id))
			return false;
		return true;
	}
}
